<?php

namespace App\Http\Controllers;

use App\Traits\Admin\SuccessOrFailure;
use Daijulong\LaravelRoles\Permission;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Routing\Controller as BaseController;

class Controller extends BaseController
{
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests,SuccessOrFailure;

    /**
     * 检查权限，失败则终止
     *
     * @param string $permission
     * @param string $failure_message
     * @param string $relation
     * @return array|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    protected function canOrFailure($permission, $failure_message = '您无权进行此操作！', $relation = 'OR')
    {
        if (Permission::cannot($permission, $relation)) {
            if (app('request')->ajax()) {
                response($this->failure($failure_message))->send();
                exit;
            }
            return abort(403, $failure_message ?: '您无权进行此操作！');
        }
    }
}
